﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;

namespace Cobit5.BIZ.Cobit
{
    public class Supporting
    {
        #region Model
        private string _practiceid;
        private int _sequence;
        private string _supportingid;
        private string _supportingname;
        private string _supportingcomment;
        private string _supportinglink;
        private DateTime? _updatedtime;
        private string _supportingdesigner;
        private string _supportingowner;
        private string _supportingreviewer;
        private int? _frequency;
        private int? _workinghours;

        /// <summary>
        /// 
        /// </summary>
        public string practiceID
        {
            set { _practiceid = value; }
            get { return _practiceid; }
        }
        /// <summary>
        /// 
        /// </summary>
        public int sequence
        {
            set { _sequence = value; }
            get { return _sequence; }
        }
        /// <summary>
        /// DSS01.02.A
        ///DSS01.02.B
        /// </summary>
        public string supportingID
        {
            set { _supportingid = value; }
            get { return _supportingid; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string supportingName
        {
            set { _supportingname = value; }
            get { return _supportingname; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string supportingComment
        {
            set { _supportingcomment = value; }
            get { return _supportingcomment; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string supportingLink
        {
            set { _supportinglink = value; }
            get { return _supportinglink; }
        }
        /// <summary>
        /// 
        /// </summary>
        public DateTime? updatedTime
        {
            set { _updatedtime = value; }
            get { return _updatedtime; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string supportingDesigner
        {
            set { _supportingdesigner = value; }
            get { return _supportingdesigner; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string supportingOwner
        {
            set { _supportingowner = value; }
            get { return _supportingowner; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string supportingReviewer
        {
            set { _supportingreviewer = value; }
            get { return _supportingreviewer; }
        }
        /// <summary>
        /// 
        /// </summary>
        public int? frequency
        {
            set { _frequency = value; }
            get { return _frequency; }
        }
        /// <summary>
        /// 
        /// </summary>
        public int? workingHours
        {
            get { return _workinghours; }
            set { _workinghours = value; }
        }
        #endregion Model

        #region SupportingFunctions

        /// <summary>
        /// Initialize
        /// </summary>
        public Supporting(string in_supportingID)
        {
            if (Supporting.ExistSupportingTemplate(in_supportingID) == 0)
                return;

            Model.SupportingTemplate mySupport = new Model.SupportingTemplate();
            DAL.SupportingTemplate optSupport = new DAL.SupportingTemplate();
            mySupport = optSupport.GetModel(in_supportingID);

            this._practiceid = mySupport.practiceID;
            this._sequence = mySupport.sequence;
            this._supportingid = mySupport.supportingID;
            this._supportingname = mySupport.supportingName;
            this._supportingcomment = mySupport.supportingComment;
            this._supportinglink = mySupport.supportingLink;
            this._updatedtime = mySupport.updatedTime;
            this._supportingdesigner = mySupport.supportingDesigner;
            this._supportingowner = mySupport.supportingOwner;
            this._supportingreviewer = mySupport.supportingReviewer;
            this._frequency = mySupport.frequency;
            this._workinghours = mySupport.workingHours;
        }

        /// <summary>
        /// Count the number of SupportingTemplate
        /// </summary>
        public static int CountAllSupportingTemplate()
        {
            DAL.SupportingTemplate optSupport = new DAL.SupportingTemplate();
            return optSupport.GetRecordCount("1=1");
        }

        /// <summary>
        /// List all the SupportingTemplate in pages
        /// </summary>
        public static List<Supporting> GetAllSupportingTemplate(string in_orderby, int in_startIndex, int in_endIndex)
        {
            DAL.SupportingTemplate optSupport = new DAL.SupportingTemplate();
            DataSet ds = optSupport.GetListByPage("1=1", in_orderby, in_startIndex, in_endIndex);
            if (ds == null || ds.Tables[0].Rows.Count < 1)
                return null;

            List<Supporting> returnSupportingTemplates = new List<Supporting>();
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                Supporting newSupporting = new Supporting(ds.Tables[0].Rows[i]["supportingID"].ToString());
                returnSupportingTemplates.Add(newSupporting);
            }

            return returnSupportingTemplates;
        }

        /// <summary>
        /// Check if the SupportingTemplate exists
        /// Return  1:Exist;
        ///         0:Not Exist;
        /// </summary>
        public static int ExistSupportingTemplate(string in_supportingID)
        {
            DAL.SupportingTemplate optSupport = new DAL.SupportingTemplate();
            if (!optSupport.Exists(in_supportingID))
                return 0;
            else return 1;
        }

        /// <summary>
        /// Create a new SupportingTemplate
        /// Return  1.Success
        ///         2.Id already existed
        ///         3.DAL Error
        /// </summary>
        public static int CreateSupportingTemplate(string in_practiceID, int in_sequence, string in_supportingID, string in_supportingName, string in_supportingComment, string in_supportingLink, string in_supportingDesigner, string in_supportingOwner, string in_supportingReviewer, int in_frequency, int in_workingHours)
        {
            if (Supporting.ExistSupportingTemplate(in_supportingID) == 1)
                return 2;

            Model.SupportingTemplate mySupport = new Model.SupportingTemplate();
            mySupport.practiceID = in_practiceID;
            mySupport.sequence = in_sequence;
            mySupport.supportingID = in_supportingID;
            mySupport.supportingName = in_supportingName;
            mySupport.supportingComment = in_supportingComment;
            mySupport.supportingLink = in_supportingLink;
            mySupport.updatedTime = DateTime.Now;
            mySupport.supportingDesigner = in_supportingDesigner;
            mySupport.supportingOwner = in_supportingOwner;
            mySupport.supportingReviewer = in_supportingReviewer;
            mySupport.frequency = in_frequency;
            mySupport.workingHours = in_workingHours;

            DAL.SupportingTemplate optSupport = new DAL.SupportingTemplate();
            bool ok = optSupport.Add(mySupport);
            if (!ok) return 3;
            else return 1;
        }

        /// <summary>
        /// Update the SupportingTemplate
        /// Return: 1.Success
        ///         2.DAL Error
        /// </summary>
        public int UpdateSupportingTemplate()
        {
            Model.SupportingTemplate mySupport = new Model.SupportingTemplate();
            mySupport.practiceID = this._practiceid;
            mySupport.sequence = this._sequence;
            mySupport.supportingID = this._supportingid;
            mySupport.supportingName = this._supportingname;
            mySupport.supportingComment = this._supportingcomment;
            mySupport.supportingLink = this._supportinglink;
            mySupport.updatedTime = DateTime.Now;
            mySupport.supportingDesigner = this._supportingdesigner;
            mySupport.supportingOwner = this._supportingowner;
            mySupport.supportingReviewer = this._supportingreviewer;
            mySupport.frequency = this._frequency;
            mySupport.workingHours = this._workinghours;

            DAL.SupportingTemplate optSupport = new DAL.SupportingTemplate();
            bool ok = optSupport.Update(mySupport);
            if (!ok) return 2;
            else return 1;
        }

        /// <summary>
        /// Delete one SupportingTemplate
        /// Return: 2.Not Exist Supporting Template
        ///         3.DAL Error in deleting the supporting template
        ///         1.Success
        /// </summary>
        public static int DeleteSupportingTemplate(string in_supportingID)
        {
            if (Supporting.ExistSupportingTemplate(in_supportingID) == 0)
                return 2;

            DAL.SupportingTemplate optSupporting = new DAL.SupportingTemplate();
            bool ok = optSupporting.Delete(in_supportingID);
            if (!ok) return 3;
            else return 1;
        }

        #endregion SupportingFunctions

        #region Supporting-TimeAlarm

        /// <summary>
        /// Get the number of TimeAlarm that belong to this Supporting
        /// </summary>
        public int CountTimeAlarm()
        {
            DAL.TimeAlarm optTA = new DAL.TimeAlarm();
            return optTA.GetRecordCount("supportingID='" + this._supportingid + "'");
        }

        /// <summary>
        /// Get the list of TimeAlarm
        /// Parameters: 1.in_orderBy:"practiceID","sequence","alarmDeadline","alarmStatus"
        /// </summary>
        public List<TimeAlarm> TimeAlarms(string in_orderBy, int in_startIndex, int in_endIndex)
        {
            DAL.TimeAlarm optTA = new DAL.TimeAlarm();
            DataSet ds = optTA.GetListByPage("supportingID='" + this._supportingid + "'", in_orderBy, in_startIndex, in_endIndex);
            if (ds == null || ds.Tables[0].Rows.Count < 1)
                return null;

            List<TimeAlarm> returnTimeAlarms = new List<TimeAlarm>();
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                Guid temp_id = new Guid(ds.Tables[0].Rows[i]["_id"].ToString());
                TimeAlarm newTimeAlarm = new TimeAlarm(temp_id);
                returnTimeAlarms.Add(newTimeAlarm);
            }

            return returnTimeAlarms;
        }

        /// <summary>
        /// Refer to TimeAlarm (CreateTimeAlarm)
        /// Return  1.Success
        ///         3.DAL Error
        /// </summary>
        public int AddTimeAlarm(DateTime in_alarmStart, DateTime in_alarmDeadline)
        {
            return TimeAlarm.CreateTimeAlarm(this._practiceid, this._sequence, this._supportingid, in_alarmStart, in_alarmDeadline);
        }

        /// <summary>
        /// Refer to TimeAlarm (DeleteTimeAlarm)
        /// Return: 1.Success
        ///         2.GUID not exist
        ///         3.DAL Error
        /// </summary>
        public int RemoveTimeAlarm(Guid in_id)
        {
            return TimeAlarm.DeleteTimeAlarm(in_id);
        }

        #endregion Supporting-TimeAlarm
    }
}
